﻿using System.Runtime.InteropServices;
using System.Xml.Schema;
using Google.Protobuf.WellKnownTypes;
using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Mysqlx.Crud;

namespace ConsoleApp10
{
    public class OrderingContext : DbContext
    {
        public OrderingContext() : base("OrderDataBase")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<OrderingContext>());
        }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetails> Details { get; set; }
        public DbSet<Goods> Goods { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 配置 Order 和 OrderDetails 的一对多关系
            modelBuilder.Entity<Order>()
                .HasMany(o => o.OrderDetails)
                .WithRequired(od => od.Order)
                .HasForeignKey(od => od.OrderId);

            base.OnModelCreating(modelBuilder);
        }
    }
    public class OrderService
    {
        private int numofOrders;
        private Customer user;
        public string Buyer { get => user.Name; set { user = new Customer(value); } }
        public OrderService(string buyer="")
        {
            using(var db=new OrderingContext()) { numofOrders = db.Orders.Count(); }
            user = new Customer(buyer);
            Buyer = buyer;
        }
        private bool IsEqual(Order order)
        {
            Order newOne = FindOrders(order.id);
                return newOne != null;
        }
        public void AddOrder(Order order)
        {
            if (order.id < 0 || order.GetnumofDetails() == 0)
            {
                throw new Exception("Error");
            }
            if (numofOrders == 0)
            {
                using (var db = new OrderingContext())
                {
                    db.Orders.Add(order);
                    db.SaveChanges();
                }
                numofOrders++;
                //Console.WriteLine("订单添加成功");
            }
            else
            {
                if (IsEqual(order))
                {
                    // Console.WriteLine("订单添加失败");
                    return;
                }
                else
                {
                    using (var db = new OrderingContext())
                    {
                        db.Orders.Add(order);
                        db.SaveChanges();
                    }
                    numofOrders++;
                    //Console.WriteLine("订单添加成功");
                }
            }
        }

        public void AddOrder(int id, List<OrderDetails> orderDetails)
        {
            Order order = new Order(id, user, orderDetails);
            if (numofOrders == 0)
            {
                using (var db = new OrderingContext())
                {
                    db.Orders.Add(order);
                    db.SaveChanges();
                }
                numofOrders++;
                //Console.WriteLine("订单添加成功");
            }
            else
            {
                if (IsEqual(order))
                {
                    // Console.WriteLine("订单添加失败");
                    return;
                }
                else
                {
                    using (var db = new OrderingContext())
                    {
                        db.Orders.Add(order);
                        db.SaveChanges();
                    }
                    numofOrders++;
                    //Console.WriteLine("订单添加成功");
                }
            }
        }
        private void IsLegal(int n)
        {
            if (n < 0)
                throw new Exception("无效账单号");
        }
        public void DelOrder(int id)
        {
            IsLegal(id);
            using (var db = new OrderingContext())
            {
                var orders = db.Orders.Include(b => b.OrderDetails).SingleOrDefault(b => b.id == id);
                if (orders != null)
                {
                    db.Orders.Remove(orders);
                    db.SaveChanges();
                }
            }
            numofOrders--;
            // Console.WriteLine("删除成功");
        }
        public void EditOrder(int id,Order newOrder)
        {
            IsLegal(id);
            if (IsEqual(newOrder))
                using(var db=new OrderingContext())
                {
                    db.Entry(newOrder).State = EntityState.Modified;
                    db.SaveChanges();
                }
            else
                AddOrder(newOrder);
        }
        public Order FindOrders(int id)
        {
            using(var db=new OrderingContext())
            {
                var orders= db.Orders.SingleOrDefault(b => b.id == id);
                if (orders != null)
                    return orders;
            }
            return null;
        }
        public List<Order> FindOrders(string name)
        {
            using (var db = new OrderingContext())
            {
                var orders = db.Orders.Where(b => b.Customer == name).ToList();
                if (orders != null)
                    return orders;
            }
            return null;
        }
    }
}
